import pygame
import math
from config import *

class Player:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.width = PLAYER_WIDTH
        self.height = PLAYER_HEIGHT
        self.color = GREEN  # 绿色
        self.speed = PLAYER_SPEED
        self.health = PLAYER_HEALTH
        self.max_health = PLAYER_HEALTH
        self.attack_power = PLAYER_ATTACK_POWER
        self.is_attacking = False
        self.attack_duration = PLAYER_ATTACK_DURATION
        self.alive = True  # 玩家存活状态
        
        # 攻击特效相关属性
        self.attack_effect_time = 0
        self.attack_effect_pos = (0, 0)
        
        # 跳跃和重力相关属性
        self.velocity_y = 0
        self.on_ground = False
        self.facing_right = True  # 玩家面向方向
        
        self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
        
    def move(self, dx, dy):
        # 检查玩家是否存活
        if not self.alive:
            return
            
        # 设置面向方向
        if dx > 0:
            self.facing_right = True
        elif dx < 0:
            self.facing_right = False
            
        # 水平移动
        self.x += dx
        
        # 更新矩形位置
        self.rect.x = self.x
        
        # 边界检测（仅水平）
        if self.x < 0:
            self.x = 0
        elif self.x > WORLD_WIDTH - self.width:
            self.x = WORLD_WIDTH - self.width
            
    def jump(self):
        # 检查玩家是否存活
        if not self.alive:
            return
            
        # 只有在地面上才能跳跃
        if self.on_ground:
            self.velocity_y = -PLAYER_JUMP_POWER
            self.on_ground = False
    
    def attack(self):
        # 检查玩家是否存活
        if not self.alive:
            return
            
        # 发起攻击
        self.is_attacking = True
        self.attack_duration = PLAYER_ATTACK_DURATION  # 攻击持续时间
        self.attack_effect_time = ATTACK_EFFECT_DURATION  # 攻击特效持续时间
        
        # 设置攻击特效位置
        if self.facing_right:
            self.attack_effect_pos = (self.x + self.width + PLAYER_ATTACK_RANGE//2, self.y + self.height//2)
        else:
            self.attack_effect_pos = (self.x - PLAYER_ATTACK_RANGE//2, self.y + self.height//2)
    
    def update(self, platforms):
        # 检查玩家是否存活
        if not self.alive:
            return
            
        # 应用重力
        self.velocity_y += PLAYER_GRAVITY
        self.y += self.velocity_y
        
        # 更新矩形位置
        self.rect.y = self.y
        
        # 检查平台碰撞
        self.on_ground = False
        for platform in platforms:
            if self.rect.colliderect(platform):
                # 从上方落到平台上
                if self.velocity_y > 0 and self.rect.bottom > platform.top and self.rect.top < platform.top:
                    self.rect.bottom = platform.top
                    self.y = self.rect.y
                    self.velocity_y = 0
                    self.on_ground = True
                # 从下方撞到平台
                elif self.velocity_y < 0 and self.rect.top < platform.bottom and self.rect.bottom > platform.bottom:
                    self.rect.top = platform.bottom
                    self.y = self.rect.y
                    self.velocity_y = 0
        
        # 地面边界检测
        if self.y > SCREEN_HEIGHT - self.height:
            self.y = SCREEN_HEIGHT - self.height
            self.rect.y = self.y
            self.velocity_y = 0
            self.on_ground = True
            
        if self.y < 0:
            self.y = 0
            self.rect.y = self.y
            self.velocity_y = 0
        
        # 更新玩家状态
        if self.is_attacking:
            self.attack_duration -= 1
            if self.attack_duration <= 0:
                self.is_attacking = False
                
        # 更新攻击特效
        if self.attack_effect_time > 0:
            self.attack_effect_time -= 1
    
    def take_damage(self, damage):
        # 受到伤害
        self.health -= damage
        if self.health < 0:
            self.health = 0
            
        # 检查玩家是否死亡
        if self.health <= 0:
            self.alive = False
    
    def get_attack_rect(self):
        # 获取攻击矩形（仅在玩家面前）
        if self.facing_right:
            return pygame.Rect(self.x + self.width, self.y, PLAYER_ATTACK_RANGE, self.height)
        else:
            return pygame.Rect(self.x - PLAYER_ATTACK_RANGE, self.y, PLAYER_ATTACK_RANGE, self.height)
    
    def draw(self, screen, camera):
        # 应用摄像机偏移
        draw_rect = camera.apply(self)
        
        # 绘制玩家（使用圆形表示）
        pygame.draw.rect(screen, self.color, draw_rect)
        pygame.draw.circle(screen, (0, 100, 0), (int(draw_rect.centerx), int(draw_rect.centery)), 
                          min(self.width, self.height)//2)
        
        # 如果正在攻击且存活，绘制攻击效果（仅在玩家面前）
        if self.is_attacking and self.alive:
            attack_rect = self.get_attack_rect()
            # 应用摄像机偏移到攻击矩形
            attack_rect.x += camera.camera.x
            attack_rect.y += camera.camera.y
            pygame.draw.rect(screen, YELLOW, attack_rect, 3)  # 黄色边框表示攻击
            
            # 绘制攻击特效（波纹效果）
            if self.attack_effect_time > 0:
                effect_radius = 20 - self.attack_effect_time
                effect_pos = (self.attack_effect_pos[0] + camera.camera.x, 
                             self.attack_effect_pos[1] + camera.camera.y)
                pygame.draw.circle(screen, ORANGE, effect_pos, effect_radius, 2)
        
        # 绘制血条
        health_bar_width = 50
        health_bar_height = 5
        health_ratio = self.health / self.max_health
        pygame.draw.rect(screen, RED, 
                        (draw_rect.x, draw_rect.y - 10, health_bar_width, health_bar_height))
        pygame.draw.rect(screen, GREEN, 
                        (draw_rect.x, draw_rect.y - 10, health_bar_width * health_ratio, health_bar_height))